/**
 * 
 */
package py.edu.ucom.cadira.game.event.war;

import java.util.List;

import org.jdom.Document;
import org.jdom.Element;

import py.edu.ucom.cadira.game.war.Board;
import py.edu.ucom.cadira.game.war.Square;
import py.edu.ucom.cadira.game.war.Tab;
import py.edu.ucom.cadira.game.war.WarUser;

/**
 * @author largonet
 * 
 */
public class SetTabUserEvent extends WarGameEvent {
	private Tab[] tabs;
    private WarUser user;
    private Board table;

    public SetTabUserEvent() {
        
        }
    public SetTabUserEvent(Board table, Tab[] tabs, WarUser user) {
        this.table = new Board();
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 10; j++) {
                this.table.setSquareInBoard(i, j, new Square());
            }
        }
        this.tabs = tabs;
        this.user = user;
    }
    public Board getTable() {
        return table;
    }
    public void setTable(Board table) {
        this.table = table;
    }
    public Tab[] getTabs() {
        return tabs;
    }
    public void setTabs(Tab[] tabs) {
        this.tabs = tabs;
    }
    public WarUser getUser() {
        return user;
    }
    public void setUser(WarUser user) {
        this.user = user;
    }
    @SuppressWarnings("unchecked")
	@Override
	public void fromXML(Document xml) {
		Element root = xml.getRootElement();
		Element setTabNode = root.getChild("SetTabUserEvent");
		Element tabsNode = setTabNode.getChild("tabs");
		List<Element> tabsChildrenNode = tabsNode.getChildren();
		for(int i=0; i<tabsChildrenNode.size(); i++){
			Element tabNode = tabsChildrenNode.get(i);
			tabs[i].fromXml(tabNode);
		}
		
		Element userNode = setTabNode.getChild("usuario");
		user.fromXml(userNode);
		
		Element boardNode = new Element("tablero");
		table.fromXml(boardNode);
	}
	@Override
	public void toXML(Element root) {
		Element setTabNode = new Element("SetTabUserEvent");
		Element tabsNode = new Element("tabs");
		for(int i=0; i<tabs.length;i++){
			Tab tab = tabs[i];
			Element tabNode = new Element("tab");
			tab.toXml(tabNode);
			tabsNode.addContent(tabNode);
		}
		
		Element userNode = user.toXml();
		
		Element boardNode = new Element("tablero");
		table.toXml(boardNode);
		
		setTabNode.addContent(tabsNode);
		setTabNode.addContent(userNode);
		setTabNode.addContent(boardNode);
		
		root.addContent(setTabNode);
	}
	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof SetTabUserEvent)) {
			return false;
		}
		SetTabUserEvent otherObj = (SetTabUserEvent) obj;
		if (otherObj.getUser().equals(getUser())) {
			return true;
		}
		return false;
	}
}
